﻿using Nop.Core;
using Nop.Plugin.Widgets.GoogleCustomSearch.Models;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Services.Stores;
using Nop.Web.Framework.Controllers;
using System;
using System.Web.Mvc;

namespace Nop.Plugin.Widgets.GoogleCustomSearch.Controllers
{
    public class WidgetsGoogleCustomSearchController : BasePluginController
    {
        private readonly IWorkContext _workContext;
        private readonly IStoreContext _storeContext;
        private readonly IStoreService _storeService;
        private readonly ISettingService _settingService;
        private readonly ILocalizationService _localizationService;

        public WidgetsGoogleCustomSearchController(IWorkContext workContext,
            IStoreContext storeContext,
            IStoreService storeService,
            ISettingService settingService,
            ILocalizationService localizationService)
        {
            this._workContext = workContext;
            this._storeContext = storeContext;
            this._storeService = storeService;
            this._settingService = settingService;
            this._localizationService = localizationService;
        }

        [AdminAuthorize]
        [ChildActionOnly]
        public ActionResult Configure()
        {
            //load settings for a chosen store scope
            var storeScope = this.GetActiveStoreScopeConfiguration(_storeService, _workContext);
            var googleCustomSearchSettings = _settingService.LoadSetting<GoogleCustomSearchSettings>(storeScope);
            var model = new ConfigurationModel();
            model.TrackingScript = googleCustomSearchSettings.TrackingScript;

            model.ActiveStoreScopeConfiguration = storeScope;
            if (storeScope > 0)
            {
                model.TrackingScript_OverrideForStore = _settingService.SettingExists(googleCustomSearchSettings, x => x.TrackingScript, storeScope);
            }

            return View("~/Plugins/Widgets.GoogleCustomSearch/Views/Configure.cshtml", model);
        }

        [HttpPost]
        [AdminAuthorize]
        [ChildActionOnly]
        public ActionResult Configure(ConfigurationModel model)
        {
            //load settings for a chosen store scope
            var storeScope = this.GetActiveStoreScopeConfiguration(_storeService, _workContext);
            var googleCustomSearchSettings = _settingService.LoadSetting<GoogleCustomSearchSettings>(storeScope);
            googleCustomSearchSettings.TrackingScript = model.TrackingScript;

            /* We do not clear cache after each setting update.
             * This behavior can increase performance because cached settings will not be cleared 
             * and loaded from database after each update */

            if (model.TrackingScript_OverrideForStore || storeScope == 0)
                _settingService.SaveSetting(googleCustomSearchSettings, x => x.TrackingScript, storeScope, false);
            else if (storeScope > 0)
                _settingService.DeleteSetting(googleCustomSearchSettings, x => x.TrackingScript, storeScope);

            //now clear settings cache
            _settingService.ClearCache();

            SuccessNotification(_localizationService.GetResource("Admin.Plugins.Saved"));

            return Configure();
        }

        [ChildActionOnly]
        public ActionResult PublicInfo(string widgetZone, object additionalData = null)
        {
            var googleCustomSearchSettings = _settingService.LoadSetting<GoogleCustomSearchSettings>(_storeContext.CurrentStore.Id);
            var model = new PublicInfoModel();
            model.TrackingScript = googleCustomSearchSettings.TrackingScript;

            return View("~/Plugins/Widgets.GoogleCustomSearch/Views/PublicInfo.cshtml", model);
        }       
      
    }
}

